package com.shuaiche.yeguan.net.http;


import com.shuaiche.yeguan.config.SCAppConfig;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Headers;

/**
 * 网络请求调用类
 */
public class HttpHelper {
    public static final int REQUEST_GET = 1;
    public static final int REQUEST_POST_PARAMS = 2;
    public static final int REQUEST_POST_JSON = 3;
    public static final int REQUEST_POST_FILE = 4;
    public static final int REQUEST_DOWNLOAD_FILE = 5;
    private int mRequestType = REQUEST_GET;

    private static HttpHelper instance;
    private static String mUrl = "";
    private String jsonParams = "";
    private static Map<String,Object> mParams = null;
    private static Map<String,String> headerParams = null;
    private static Object tag = null;
    private static File[] mFiles = null;
    private static IHttpProcess mIhttpProcess;
    private static String savePath;
    private static String fileName;
    private static DownloadListener downloadListener;
    private static ProgressListener progressListener;

    private static int apiType = 0;


    private CallBack defaultCallback = new CallBack(){
        @Override
        public void onStart() {

        }

        @Override
        public void onFinish() {

        }

        @Override
        public void onSuccess(String result, Headers headers) {

        }

        @Override
        public void onSuccess(String result) {

        }

        @Override
        public void onFailure(Exception e) {

        }

    };

    public HttpHelper(){

    }

    public static HttpHelper getInstance() {
        if (instance == null) {
            synchronized (HttpHelper.class) {
                if (instance == null) {
                    instance = new HttpHelper();
                }
            }
        }else {
            synchronized (HttpHelper.class) {
                initParams();
            }

        }
        return instance;
    }

    /**
     * 初始化请求参数
     */
    private static void initParams() {
        if (apiType == 0 || apiType == 1){
            mUrl = SCAppConfig.BASE_URL_DEV;
        }else if (apiType == 2){
            mUrl = SCAppConfig.BASE_URL_TEST;
        }else if (apiType == 3){
            mUrl = SCAppConfig.BASE_URL_RELEASE;
        }
        mParams = null;
        tag = null;
        mFiles = null;
    }

    private static void setApiType(int type){
        apiType = type;
    }

    /**
     * 初始化网络请求
     * @param httpProcess 网络请求控制器
     */
    public static void init(IHttpProcess httpProcess){
        mIhttpProcess = httpProcess;
    }

    /**
     * 初始化网络请求
     * @param httpProcess 网络请求控制器
     * @param type 0 1 开发环境 2测试 3正式
     */
    public static void init(IHttpProcess httpProcess,int type){
        mIhttpProcess = httpProcess;
        apiType = type;

        if (apiType == 0 || apiType == 1){
            mUrl = SCAppConfig.BASE_URL_DEV;
        }else if (apiType == 2){
            mUrl = SCAppConfig.BASE_URL_TEST;
        }else if (apiType == 3){
            mUrl = SCAppConfig.BASE_URL_RELEASE;
        }
    }

    /**
     * 传入服务器地址
     * @param url 服务器地址
     * @return
     */
    public HttpHelper url(String url){
        mUrl = url;
        return this;
    }

    /**
     * 传入url后半段地址
     * @param url url后半段拼接地址
     * @return
     */
    public HttpHelper appendUrl(String url){
        mUrl += url;
        return this;
    }

    /**
     * 添加请求标记
     * @param object 标记
     * @return
     */
    public HttpHelper tag(Object object){
        tag = object;
        return this;
    }

    /**
     * post请求添加json字符串参数
     * @param jsonStr json字符串
     * @return
     */
    public HttpHelper addJsonParam(String jsonStr){
        jsonParams = jsonStr;
        return this;
    }

    /**
     * post请求添加json字符串参数
     * @param jsonMap Map集合
     * @return
     */
    public HttpHelper addJsonParam(Map<String,Object> jsonMap){

        JSONObject jsonObject = new JSONObject();
        try {
            for (Map.Entry<String, Object> entry : jsonMap.entrySet()) {
                String key = entry.getKey();
                Object value = entry.getValue();
                jsonObject.put(key,value);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        jsonParams = jsonObject.toString();
        return this;
    }


    /**
     * get请求添加参数
     * @param params map键值对集合
     * @return
     */
    public HttpHelper addGetParam(Map<String,Object> params){
        mParams = params;
        return this;
    }

    /**
     * post请求添加参数
     * @param params map键值对集合
     * @return
     */
    public HttpHelper addPostParam(Map<String,Object> params){
        mParams = params;
        return this;
    }

    public HttpHelper addSavePath(String path){
        savePath = path;
        return this;
    }

    public HttpHelper addFileName(String fileName){
        this.fileName = fileName;
        return this;
    }

    public HttpHelper addDownLoadListener(DownloadListener listener){
        downloadListener = listener;
        return this;
    }

    public HttpHelper addTokenParam(Map<String, Object> params,String token) {
        if (params == null)
        {
            params = new HashMap<>();  
        }
        params.put("token", token);
        addPostParam(params);
        return this;
    }

    public void tokenPost(CallBack callback) {

        paramPost(callback);

    }
    /**
     * 添加请求头
     * @param params 添加请求头
     * @return
     */
    public HttpHelper addHeader(Map<String,String> params){
        headerParams = params;
        return this;
    }

    /**
     * 上传文件添加文件列表参数
     * @param files file[]
     * @return
     */
    public HttpHelper addFiles(File[] files){
        mFiles = files;
        return this;
    }

    /**
     * get请求
     * @param callback 请求回调
     */
    public void get(CallBack callback){
        mRequestType = REQUEST_GET;
        request(mRequestType,callback);
    }

    /**
     * post传参请求
     * @param callback 请求回调
     */
    public void paramPost(CallBack callback){
        mRequestType = REQUEST_POST_PARAMS;
        request(mRequestType,callback);
    }

    /**
     * post传json字符串请求
     * @param callback 请求回调
     */
    public void jsonPost(CallBack callback){
        mRequestType = REQUEST_POST_JSON;
        request(mRequestType,callback);
    }

    /**
     * post提交文件请求
     * @param callback 请求回调
     */
    public void uploadFile(CallBack callback){
        mRequestType = REQUEST_POST_FILE;
        request(mRequestType,callback);
    }

    public void downloadFile(){
        mRequestType = REQUEST_DOWNLOAD_FILE;
        request(mRequestType,null);
    }
    public HttpHelper addProgressListener(ProgressListener listener){
        progressListener = listener;
        return this;
    }



    public void request(int requestType,CallBack callback){
        if(callback == null){
            callback = defaultCallback;
        }

        switch(requestType){
            case REQUEST_GET:
                mIhttpProcess.get(mUrl,mParams,tag,headerParams,callback);
                break;
            case REQUEST_POST_PARAMS:
                mIhttpProcess.post(mUrl,mParams,tag,headerParams,callback);
                break;
            case REQUEST_POST_JSON:
                mIhttpProcess.post(mUrl,jsonParams,tag,headerParams,callback);
                break;
            case REQUEST_POST_FILE:
                mIhttpProcess.uploadFile(mUrl,mParams,tag,headerParams,callback,mFiles);
                break;
            case REQUEST_DOWNLOAD_FILE:
                mIhttpProcess.downloadFile(mUrl,savePath,fileName,tag,headerParams,downloadListener,progressListener);
                break;
        }

    }
}
